//
//  JCSBTableViewController.m
//  Tunnel
//
//  Created by wbb on 2025/6/28.
//

#import "JCSBTableViewController.h"
#import "BRPickerView.h"
#import "TZImagePickerController.h"
#import "DangerTableVC.h"
#import "MultiLevelPickerView.h"
#import "SDWebImage.h"
#import "DangerResultVC.h"

typedef enum : NSUInteger {
    TYTujian, // 土建
    TYJidian, // 机电
    TYNone, // 默认
} TYCheckObj;

@interface JCSBTableViewController ()
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *deptLab;
@property (weak, nonatomic) IBOutlet UILabel *startTImeLab;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIButton *bedBtn;
@property (weak, nonatomic) IBOutlet UIButton *severityBtn;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLab;
@property (weak, nonatomic) IBOutlet UITextField *describeTF;
@property (weak, nonatomic) IBOutlet UIImageView *addImgView;
@property (weak, nonatomic) IBOutlet UIImageView *imgView1;
@property (weak, nonatomic) IBOutlet UIImageView *imgView2;
@property (weak, nonatomic) IBOutlet UIImageView *imgView3;
@property (weak, nonatomic) IBOutlet UIImageView *imgView4;
@property (weak, nonatomic) IBOutlet UIImageView *imgView5;
@property (weak, nonatomic) IBOutlet UIImageView *closeImgView1;
@property (weak, nonatomic) IBOutlet UIImageView *closeImgView2;
@property (weak, nonatomic) IBOutlet UIImageView *closeImgView3;
@property (weak, nonatomic) IBOutlet UIImageView *closeImgView4;
@property (weak, nonatomic) IBOutlet UIImageView *closeImgView5;
@property (weak, nonatomic) IBOutlet UILabel *jcdxLab;

@property (weak, nonatomic) IBOutlet UIImageView *arrowImgView;
@property (weak, nonatomic) IBOutlet UITextField *kszhTF;// 开始桩号
@property (weak, nonatomic) IBOutlet UITextField *jszhTF;// 结束桩号
@property (weak, nonatomic) IBOutlet UITextField *sbzhTF;// 设备桩号
@property (weak, nonatomic) IBOutlet UILabel *arrowLab;// 方向
@property (weak, nonatomic) IBOutlet UILabel *carLab;
@property (weak, nonatomic) IBOutlet UILabel *bhlxLab;// 病害类型
@property (nonatomic, strong) UIButton * selectBtn;
@property (nonatomic, assign) BOOL  isBad; // 是否异常
@property (nonatomic, assign) BOOL  isExpansion; // 是否展开
@property (nonatomic, assign) TYCheckObj  checkObj; // 检查对象
@property (nonatomic, strong) NSMutableArray * faultList; // 病害类型

@property (nonatomic, strong) NSMutableArray * dangerList; // 危害数组
@property (nonatomic, strong) NSMutableDictionary * dataDic; // 最终保存的数据

@property (nonatomic, assign) NSInteger  jcdxId; // 用来保存检查对象第三列id

@property (nonatomic, copy) NSArray * planList; //
@property (nonatomic, copy) NSArray * sysList; //
@property (nonatomic, copy) NSArray * bodyList; //

@property (nonatomic, strong) DangerTableVC * dangerVC;
@property (nonatomic, strong) MultiLevelPickerView * pickerView;
@property (nonatomic, copy) NSString * imgUrl1; //
@property (nonatomic, copy) NSString * imgUrl2;//
@property (nonatomic, copy) NSString * imgUrl3; //
@property (nonatomic, copy) NSString * imgUrl4;//
@property (nonatomic, copy) NSString * imgUrl5;//

@property (nonatomic, strong) NSMutableArray * fileIdList; // 缓存图片id

@end

@implementation JCSBTableViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"检查上报";
    self.dataDic = @{
        @"beginPileNo": @"",
        @"body": @"",
        @"bodyId": @"",
        @"categoryInsertRequestList": @[
            
        ].mutableCopy,
        @"deviceId": @"",
        @"deviceName": @"",
        @"direction": @"",
        @"endPileNo": @"",
        @"faultType": @"",
        @"faultTypeId": @0,
        @"jcfl": @"",
        @"jcflId": @"",
        @"jcjg": @"0",
        @"jcsj": [self getCurrentDate],
        @"jzsj": [self getCurrentDate],
        @"land": @0,
        @"partId": @0,
        @"parts": @"",
        @"pileNo": @"",
        @"proId": @0,
        @"proName": @"",
        @"wtfj": @"",
        @"wtms": @"",
        @"yhDeptId": @"",
        @"yhDeptName": @""
    }.mutableCopy;
    self.dangerList = @[].mutableCopy;
    self.fileIdList = @[].mutableCopy;
    
    self.dangerVC = (DangerTableVC *)self.childViewControllers[0];
    NSLog(@"%@",self.dangerVC);
    self.dangerVC.prentVC = self;
    
    self.checkObj = TYNone;
    
    self.rightBtn.selected = YES;
    self.bedBtn.selected = NO;
    self.severityBtn.selected = NO;
    self.selectBtn = self.rightBtn;
    
    self.startTImeLab.text = [self getCurrentDate];
    self.endTimeLab.text = [self getCurrentDate];
    
    [self.describeTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [self.kszhTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [self.jszhTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [self.sbzhTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.imgView1.superview.hidden = YES;
    self.imgView2.superview.hidden = YES;
    self.imgView3.superview.hidden = YES;
    self.imgView4.superview.hidden = YES;
    self.imgView5.superview.hidden = YES;
    [self.addImgView addTarget:self action:@selector(addImgClick)];
//    [self.closeImgView1 addTarget:self action:@selector(closeImg1)];
//    [self.closeImgView2 addTarget:self action:@selector(closeImg2)];
//    [self.closeImgView3 addTarget:self action:@selector(closeImg3)];
//    [self.closeImgView4 addTarget:self action:@selector(closeImg4)];
//    [self.closeImgView5 addTarget:self action:@selector(closeImg5)];
    self.closeImgView1.userInteractionEnabled = YES;
    self.closeImgView2.userInteractionEnabled = YES;
    self.closeImgView3.userInteractionEnabled = YES;
    self.closeImgView4.userInteractionEnabled = YES;
    self.closeImgView5.userInteractionEnabled = YES;
    
    [self getPlanDictionaryList];
    
}
// 获取巡检养护字典列表
- (void)getPlanDictionaryList {
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetPlanDictionaryList parameters:@{@"levelType":@"巡检",@"proId":@"1"} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        NSArray *arr = data[@"data"];
        weakSelf.planList = [NSArray arrayWithArray:arr];
        NSString *planDictionaryId = [NSString stringWithFormat:@"%@",arr[0][@"id"]];
        [weakSelf getSystemDictionaryList:planDictionaryId ];
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
}
- (void)getSystemDictionaryList:(NSString *)planDictionaryId {
    kWEAK_SELF
    [TYNetWorkTool postRequest:TYGetSystemDictionaryList parameters:@{@"levelType":@"巡检",@"proId":@"1",@"planDictionaryId":planDictionaryId?:@""} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        NSArray *arr = data[@"data"];
        weakSelf.sysList = [NSArray arrayWithArray:arr];
        
        NSString *systemDictionaryId = [NSString stringWithFormat:@"%@",arr[0][@"id"]];
        [weakSelf getBodyDictionaryList:systemDictionaryId];
        
        
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
}

- (void)getBodyDictionaryList:(NSString *)systemDictionaryId {
    kWEAK_SELF
    [TYNetWorkTool postRequest:TYGetBodyDictionaryList parameters:@{@"levelType":@"巡检",@"proId":@"1",@"systemDictionaryId":systemDictionaryId?:@""} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        weakSelf.bodyList = [NSArray arrayWithArray:data[@"data"]];
        
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
}

- (void)textChange:(UITextField *)tf {
    if(tf == self.describeTF) {
        self.dataDic[@"wtms"] = tf.text;
    }else if(tf == self.kszhTF) {
        self.dataDic[@"beginPileNo"] = tf.text;
    }else if(tf == self.jszhTF) {
        self.dataDic[@"endPileNo"] = tf.text;
    }else if (tf == self.sbzhTF) {
        self.dataDic[@"pileNo"] = tf.text;
        
    }
}
-(void)closeImg1{
    self.imgView1.superview.hidden = true;
    self.addImgView.hidden = NO;
    [self.fileIdList removeObject:self.imgUrl1];
}
-(void)closeImg2{
    self.imgView2.superview.hidden = true;
    self.addImgView.hidden = NO;
    [self.fileIdList removeObject:self.imgUrl2];

}
-(void)closeImg3{
    self.imgView3.superview.hidden = true;
    self.addImgView.hidden = NO;
    [self.fileIdList removeObject:self.imgUrl3];

}
-(void)closeImg4{
    self.imgView4.superview.hidden = true;
    self.addImgView.hidden = NO;
    [self.fileIdList removeObject:self.imgUrl4];

}
-(void)closeImg5{
    self.imgView5.superview.hidden = true;
    self.addImgView.hidden = NO;
    [self.fileIdList removeObject:self.imgUrl5];

}

- (IBAction)closeClick1:(UIButton *)sender {
    [self closeImg1];
}
- (IBAction)closeClick2:(UIButton *)sender {
    [self closeImg2];
}
- (IBAction)closeClick3:(UIButton *)sender {
    [self closeImg3];
}
- (IBAction)closeClick4:(id)sender {
    [self closeImg4];
}
- (IBAction)closeClick5:(id)sender {
    [self closeImg5];
}

// 添加图片
- (void)addImgClick {
    kWEAK_SELF;
    NSInteger count = 5 - self.fileIdList.count;
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:nil];
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.maxImagesCount = count;
    imagePickerVc.allowEditVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.presetName = AVAssetExportPresetHighestQuality;
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        for (int i = 0; i < photos.count; i++) {
            UIImage * img = photos[i];
            [TYNetWorkTool uploadFileWithOption:@{} withRequestURL:TYGetCommonUpload dataArray:@[img] dataName:@[@"file"] dataKey:@[@"file"] downloadSuccess:^(NSDictionary *   responseObject) {
                NSInteger index = weakSelf.fileIdList.count;
                if(index == 0){
                    weakSelf.imgView1.superview.hidden = false;
                    weakSelf.imgView1.image = img;
                    weakSelf.imgUrl1 = responseObject[@"fileId"];
//                    [weakSelf.fileIdList addObject:responseObject[@"fileId"]];
                }
                if(index == 1){
                    weakSelf.imgView2.superview.hidden = false;
                    weakSelf.imgView2.image = img;
                    weakSelf.imgUrl2 = responseObject[@"fileId"];
//                    [weakSelf.fileIdList addObject:responseObject[@"fileId"]];

                }
                if(index == 2){
                    weakSelf.imgView3.superview.hidden = false;
                    weakSelf.imgView3.image = img;
//                    weakSelf.imgUrl3 = responseObject[@"fileId"];

                }
                if(index == 3){
                    weakSelf.imgView4.superview.hidden = false;
                    weakSelf.imgView4.image = img;
                    weakSelf.imgUrl4 = responseObject[@"fileId"];
//                    [weakSelf.fileIdList addObject:responseObject[@"fileId"]];

                }
                if(index == 4){
                    weakSelf.imgView5.superview.hidden = false;
                    weakSelf.imgView5.image = img;
                    weakSelf.imgUrl5 = responseObject[@"fileId"];
//                    [weakSelf.fileIdList addObject:responseObject[@"fileId"]];

                }
                [weakSelf.fileIdList addObject:responseObject[@"fileId"]];
                
                if(weakSelf.fileIdList.count == 5){
                    weakSelf.addImgView.hidden = YES;
                }

            } downloadFailure:^(NSError * _Nonnull error) {
                
            } progress:^(float progress) {
                
            }];
        
        }
        

        

    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
- (IBAction)resultClick:(UIButton *)sender {
    if(sender.isSelected) return;
    
    sender.selected = !sender.selected;
    self.selectBtn.selected = NO;
    if(sender == self.rightBtn) {
        self.isBad = NO;
        self.dataDic[@"jcjg"] = @"0";
    }else if (sender == self.bedBtn) {
        self.isBad = YES;
        self.dataDic[@"jcjg"] = @"1";
    }else {
        self.isBad = YES;
        self.dataDic[@"jcjg"] = @"2";
    }
    
    self.selectBtn = sender;
    
    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:4 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
}
- (IBAction)onSubmit:(UIButton *)sender {
    if([self.dataDic[@"proName"] isEqualToString:@""]){
        [MBProgressHUD promptMessage:@"请选择检查地点" inView:self.view];
        return;
    }
    if([self.dataDic[@"yhDeptName"] isEqualToString:@""]){
        [MBProgressHUD promptMessage:@"请选择养护单位" inView:self.view];
        return;
    }
    
    NSMutableArray *categoryInsertRequestList = @[].mutableCopy;
    for (int i = 0; i<self.dangerList.count; i++) {
        NSDictionary *dic = self.dangerList[i];
        NSDictionary *item = @{
            @"propertyName": dic[@"propertyName"]?:@"",
            @"propertyValue": dic[@"propertyValue"]?:@"",
            @"typeId": dic[@"typeId"]?:@"",
            @"typeName": self.dataDic[@"faultType"]?:@"",
            @"unit": dic[@"unit"]?:@""
        };
        [categoryInsertRequestList addObject:item];
    }
    
    self.dataDic[@"categoryInsertRequestList"] = categoryInsertRequestList;
    
    self.dataDic[@"wtfj"] = [self.fileIdList componentsJoinedByString:@";"];// 图片id拼接
    
    [TYNetWorkTool postRequest:TYAdd parameters:self.dataDic successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        NSLog(@"%@",data);
        if (success) {
            [MBProgressHUD promptMessage:@"保存成功" inView:self.view];
                DangerResultVC *vc = [[DangerResultVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
        }
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        if(indexPath.row == 4) {
            return self.rightBtn.isSelected ? 0 : 44;
        }
        if(indexPath.row == 6) {
            return 95;
        }
    }
    
    if(indexPath.section == 1) {
        if(self.isBad){// 异常
            if(indexPath.row == 0) {
                return 44;
            }
            if(self.isExpansion) {// 展开
                if(self.checkObj == TYNone) {
                    if(indexPath.row == 2||indexPath.row == 3||indexPath.row == 4||indexPath.row == 7||indexPath.row == 8) {
                        return 0;
                    }
                }else if (self.checkObj == TYTujian) {// 土建
                    if(indexPath.row == 4) {
                        return 0;
                    }
                    if(indexPath.row == 8) {
                        return self.dangerList.count > 0 ? 44 * self.dangerList.count : 0;
                    }
                    
                }else if (self.checkObj == TYJidian) {// 机电
                    if(indexPath.row == 2||indexPath.row == 3) {
                        return 0;
                    }
                    
                    if(indexPath.row == 8) {
                        return self.dangerList.count > 0 ? 44 * self.dangerList.count : 0;
                    }
                }
                
                
                
                return 44;
            }else {
                return 0;
            }
            
            
        }
        
        return 0;
    }
    
    return  44;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  0.01;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){
        if (indexPath.row == 2) {
            [self showPicker:indexPath];
        }else if(indexPath.row == 0){
            [self showAddressPicker:@[@"高浪路隧道"] atIndexPath:indexPath];
        }else if(indexPath.row == 1){
            NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
            for (int i=0; i<self.deptList.count; i++) {
                NSDictionary *dic = self.deptList[i];
                [arr addObject:dic[@"deptName"]?:@""];
            }
            [self showAddressPicker:arr atIndexPath:indexPath];
            
        }else if (indexPath.row == 4) {
            [self showPicker:indexPath];
        }
    }else {
        if(indexPath.row == 0) {
            [self rotateViewBy180Degrees:self.arrowImgView] ;
            self.isExpansion = !self.isExpansion;
            [self.tableView beginUpdates];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableView endUpdates];
        }else if(indexPath.row == 1) {
            
            
            
            
            NSMutableArray * array = [NSMutableArray array];
            for (NSDictionary * dict in self.planList) {
                [array addObject:dict[@"propertyName"]];
            }
            NSMutableArray * array2 = [NSMutableArray array];
            for (NSDictionary * dict in self.sysList) {
                [array2 addObject:dict[@"propertyName"]];
            }
            NSMutableArray * array3 = [NSMutableArray array];
            for (NSDictionary * dict in self.bodyList) {
                [array3 addObject:dict[@"propertyName"]];
            }
            kWEAK_SELF;
            // 显示三级选择器并传入默认数据
            [MultiLevelPickerView showPickerWithFirstLevelData:array
                                               secondLevelData:array2
                                                thirdLevelData:array3
                                                         title:@""
                                                    completion:^(NSArray *selectedValues, NSArray *selectedIndexes) {
                //                NSLog(@"选择完成: %@", [selectedValues componentsJoinedByString:@"/"]);
                NSInteger index1 = [selectedIndexes[0] intValue];
                NSDictionary *dic1 = weakSelf.planList[index1];
                
                NSInteger index2 = [selectedIndexes[0] intValue];
                NSDictionary *dic2 = weakSelf.sysList[index2];
                
                NSInteger index3 = [selectedIndexes[0] intValue];
                NSDictionary *dic3 = weakSelf.bodyList[index3];
                
                weakSelf.jcdxId = [dic3[@"id"] integerValue];
                
                NSString *jcdxStr = [selectedValues componentsJoinedByString:@"/"];
                if([dic1[@"id"] intValue] == 6){
                    weakSelf.checkObj = TYJidian;
                }else {
                    weakSelf.checkObj = TYTujian;
                }
                weakSelf.dataDic[@"bodyId"] = [NSString stringWithFormat:@"%@/%@/%@",dic1[@"id"],dic2[@"id"],dic3[@"id"]];
                weakSelf.dataDic[@"body"] = jcdxStr;
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.jcdxLab.text = jcdxStr;
                    [weakSelf.tableView beginUpdates];
                    [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
                    [weakSelf.tableView endUpdates];
                    
                });
                
                
                
            } onCancel:^{
                NSLog(@"用户取消选择");
            } indexChanged:^(NSInteger level, NSInteger index) {
                NSLog(@"第 %ld 级索引变更为: %ld", (long)level, (long)index);
            } requestData:^(NSInteger level, NSArray *selectedValues, void (^completion)(NSArray *)) {
                // 根据级别和已选值加载下一级数据
                if(level == 1) {
                    NSDictionary *dic1 = @{};
                    for (NSDictionary * dict in self.planList) {
                        if([dict[@"propertyName"] isEqualToString:selectedValues[level-1]]){
                            dic1 = dict;
                            break;
                        }
                    }
                    
                    NSString *planDictionaryId = [NSString stringWithFormat:@"%@",dic1[@"id"]];
                    [TYNetWorkTool postRequest:TYGetSystemDictionaryList parameters:@{@"levelType":@"巡检",@"proId":@"1",@"planDictionaryId":planDictionaryId?:@""} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
                        NSArray *arr = data[@"data"];
                        weakSelf.sysList = [NSArray arrayWithArray:arr];
                        
                        NSMutableArray * array3 = [NSMutableArray array];
                        for (NSDictionary * dict in self.sysList) {
                            [array3 addObject:dict[@"propertyName"]];
                        }
                        completion(array3);
                    } failureBlock:^(NSString * _Nonnull description) {
                        
                    }];
                }else if(level == 2){
                    NSDictionary *dic1 = @{};
                    for (NSDictionary * dict in self.sysList) {
                        if([dict[@"propertyName"] isEqualToString:selectedValues[level-1]]){
                            dic1 = dict;
                            break;
                        }
                    }
                    NSString *systemDictionaryId = [NSString stringWithFormat:@"%@",dic1[@"id"]];
                    
                    [TYNetWorkTool postRequest:TYGetBodyDictionaryList parameters:@{@"levelType":@"巡检",@"proId":@"1",@"systemDictionaryId":systemDictionaryId?:@""} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
                        weakSelf.bodyList = [NSArray arrayWithArray:data[@"data"]];
                        
                        NSMutableArray * array3 = [NSMutableArray array];
                        for (NSDictionary * dict in self.bodyList) {
                            [array3 addObject:dict[@"propertyName"]];
                        }
                        completion(array3);
                    } failureBlock:^(NSString * _Nonnull description) {
                        
                    }];
                }else {
                    completion(@[]);
                }
            }];
            
            
        }else if (indexPath.row == 5) {
            NSArray *arr = @[@"南线",@"北线"];
            [self showAddressPicker:arr atIndexPath:indexPath];
            
        }else if (indexPath.row == 6) {
            NSArray *arr = @[@"1",@"2",@"3"];
            [self showAddressPicker:arr atIndexPath:indexPath];
            
        }else if (indexPath.row == 7) {
            kWEAK_SELF
            NSString *url = [NSString stringWithFormat:@"%@/%ld",TYGetFaultType,self.jcdxId];
            [TYNetWorkTool getRequest:url parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
                weakSelf.faultList = [NSMutableArray arrayWithArray:data[@"data"]];
                
                NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
                for (int i=0; i<weakSelf.faultList.count; i++) {
                    NSDictionary *dic = weakSelf.faultList[i];
                    [arr addObject:dic[@"faultName"]?:@""];
                }
                [self showAddressPicker:arr atIndexPath:indexPath];
                
            } failureBlock:^(NSString * _Nonnull description) {
                
            }];
        }
    }
    
}

/// 单列文本选择器
- (void)showAddressPicker:(NSArray *)arr atIndexPath:(NSIndexPath *)indexPath {
    
    BRTextPickerView *textPickerView = [[BRTextPickerView alloc]initWithPickerMode:BRTextPickerComponentSingle];
    //    textPickerView.title = @"地点";
    // 设置数据源
    textPickerView.dataSourceArr = arr;
    kWEAK_SELF
    textPickerView.singleResultBlock = ^(BRTextModel * _Nullable model, NSInteger index) {
        NSLog(@"选择的值：%@", model.text);
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                weakSelf.addressLab.text = model.text;
                weakSelf.dataDic[@"proName"] = model.text;
            }else if (indexPath.row == 1){
                NSDictionary *dic = self.deptList[index];
                weakSelf.deptLab.text = model.text;
                weakSelf.dataDic[@"yhDeptName"] = model.text;
                weakSelf.dataDic[@"yhDeptId"] = dic[@"bh"];
            }
        }else {
            if (indexPath.row == 5){
                weakSelf.arrowLab.text = model.text;
                weakSelf.dataDic[@"direction"] = model.text;
            }else if (indexPath.row == 6) {// 车道
                weakSelf.carLab.text = model.text;
                weakSelf.dataDic[@"land"] = model.text;
            }else if (indexPath.row == 7) {//
                NSDictionary *dic = self.faultList[index];
                weakSelf.bhlxLab.text = model.text;
                weakSelf.dataDic[@"faultType"] = model.text;
                weakSelf.dataDic[@"faultTypeId"] = dic[@"faultId"];
                
                NSString *url = [NSString stringWithFormat:@"%@/%@",TYGetFaultCategory,dic[@"faultId"]];
                [TYNetWorkTool getRequest:url parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
                    [weakSelf.dangerList removeAllObjects];
                    NSArray *arr = [NSArray arrayWithArray:data[@"data"]];
                    
                    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:obj];
                        [weakSelf.dangerList addObject:dic];
                    }];
                    weakSelf.dangerVC.dangerList = weakSelf.dangerList;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf.dangerVC.tableView reloadData];
                        [weakSelf.tableView beginUpdates];
                        [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:8 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
                        [weakSelf.tableView endUpdates];
                    });
                    
                    
                } failureBlock:^(NSString * _Nonnull description) {
                    
                }];
                
            }
        }
    };
    [textPickerView show];
}

/// 时间选择
- (void)showPicker:(NSIndexPath *)indexPath {
    // 1.创建日期选择器
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    // 2.设置属性
    datePickerView.pickerMode = BRDatePickerModeYMD;
    datePickerView.title = @"选择年月日";
    datePickerView.isAutoSelect = YES;
    kWEAK_SELF
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        NSLog(@"选择的值：%@", selectValue);
        if (indexPath.section == 0) {
            if(indexPath.row == 2){
                weakSelf.startTImeLab.text = selectValue;
                weakSelf.dataDic[@"jcsj"] =selectValue;
            }else if(indexPath.row == 4) {
                weakSelf.endTimeLab.text = selectValue;
                weakSelf.dataDic[@"jzsj"] = selectValue;
            }
        }
    };
    
    // 3.显示
    [datePickerView show];
}

// 旋转视图 180 度（使用 UIView 动画）
- (void)rotateViewBy180Degrees:(UIView *)view {
    [UIView animateWithDuration:0.5  // 动画时长
                          delay:0.0  // 延迟时间
                        options:UIViewAnimationOptionCurveEaseInOut  // 动画曲线
                     animations:^{
        // 旋转 180 度（π 弧度）
        view.transform = CGAffineTransformRotate(view.transform, M_PI);
    } completion:^(BOOL finished) {
        // 动画完成后的回调
        NSLog(@"旋转完成");
    }];
}
- (NSString *)getCurrentDate {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // 1. 设置日期格式
    formatter.dateFormat = @"yyyy-MM-dd";
    
    // 2. 设置时区（避免不同时区的时间差异）
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; // 或本地时区
    
    // 3. 设置本地化（确保格式不受地区影响）
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    
    // 4. 格式化当前日期
    NSDate *currentDate = [NSDate date];
    return [formatter stringFromDate:currentDate];
}
@end
